package xm.com.handlertest;

import java.util.concurrent.LinkedTransferQueue;


public class TestThreadMsg3 {
    public static TestThreadMsg3 MainThread;//主线程
    static LinkedTransferQueue messageQueue;//主线程的阻塞消息队列

    public TestThreadMsg3() {
        MainThread = this;
    }


    public static void main(String[] args){
        messageQueue = new LinkedTransferQueue();

        Thread1 t1 = new Thread1();
        t1.start();

        for (;;) {
            String message = null;
            try {
                message = (String) messageQueue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("主线程:"+Thread.currentThread().getId()+"  收到:"+message);
            if (message.equals("主线程去更新UI吧")){
                System.out.println("主线程:"+Thread.currentThread().getId()+"  do work!");
                System.out.println("");
                System.out.println("");
            }
        }
    }


    static class Thread1 extends Thread{
        @Override
        public void run() {
            super.run();
            for (;;) {
                try {
                    String message="主线程去更新UI吧";
                    System.out.println("子线程:"+Thread.currentThread().getId()+"  发送:"+message);
                    MainThread.messageQueue.put(message);//放到主线程的消息队列里
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
